<template>
	<view class="u-wrap">
		<u-navbar :is-back="false" :border-bottom="false">
			<view class="slot-wrap flex align-center">
				分类
			</view>
		</u-navbar>
		<!-- #ifdef H5 -->
		<u-sticky index='1' :offset-top="0" :enable="enable">
			<!-- #endif -->
			<!-- #ifndef  H5 -->
			<u-sticky index='1' :offset-top="offsetTop" h5-nav-height="44" :enable="enable">
				<!-- #endif -->
				<view class="main-top paddings justify-center">
					<view>
						<navigator url="/pages/index/search" hover-class="none">
							<view class="weui-cell__bd">
								<view class="cuIcon-search text-xl">
								</view>
								<input class="weui-input" name="input" disabled placeholder-class="placeholder-input text-df"
									placeholder="输入关键词搜索商品" />
							</view>
						</navigator>
					</view>
				</view>
			</u-sticky>
			<view class="u-menu-wrap">
				<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
					<view v-for="(item,index) in cate_list" :key="index" class="u-tab-item"
						:class="[listQuery.cid==item.id ? 'u-tab-item-active' : '']"  
						@tap.stop="swichMenu(item)">
						<text class="u-line-1">{{item.name}}</text>
					</view>
				</scroll-view>
				<scroll-view scroll-y class="right-box" >
					<u-empty text="没有该分类数据~" mode="list" margin-top="0" v-if="!list.length"></u-empty>
					<view class="page-view" v-for="(item,index) in list" :key="index" >
						<view class="page-view-box flex" @click="multiRedirect('/pages/details/details?id='+item.id)">
							<image :src="item.cover_img" mode="aspectFill"></image>
							<view class="">
								<view class="name text-cut u-font-30 text-bold">
									{{item.name}}
								</view>
								<view class="u-font-24">
									{{item.coll_intro}}
								</view>
								<view class="u-font-30 text-orange">
									{{item.free_intro}}
								</view>
							</view>
						</view>
						
						
					</view>
				</scroll-view>
			</view>
	</view>
</template>

<script>
	
	export default {
		data() {
			return {
				cate_list:[],
				
				enable: true,
				offsetTop: 0,

				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度

				listQuery:{
					keyword: '',
					cid: '',
					page: 1,
				},			
				list: [],
				total_page:false,
			}
		},
		computed: {

		},
		onLoad() {
			this.getBaseInfo()
		},
		methods: {
			getBaseInfo(){
				this.$u.http.post('/api/index/defInfo', {
					origin:"classify",
					type:'classify'
				}).then(({ data }) => {
					this.cate_list = data.cate_list||[]
					this.listQuery.cid = (this.cate_list[0]||{}).id
					this._initData(true)
					
				}).catch(v => {
					
				});
			},
			_initData(is_reset) {
				if (is_reset === true) {
					this.listQuery.page=1
					this.total_page = false;
					this.list = [];
				}
				this.$u.http.post('/api/course/lists',this.listQuery).then(({
					data
				}) => {
					this.list = this.list.concat(data.list || [])
					this.listQuery.page++
					this.total_page = data.total_page || 0		
				});
			},
			// 适配吸顶tab高度
			// setOffestTop() {
			// 	let systemInfo = uni.getSystemInfoSync()
			// 	let topPx = systemInfo.statusBarHeight + 44 // 顶部状态栏+沉浸式自定义顶部导航
			// 	this.offsetTop = topPx / (uni.upx2px(topPx) / topPx) // px转rpx
			// 	console.log(this.offsetTop)
			// },
			// getImg() {
			// 	return Math.floor(Math.random() * 35);
			// },
			// 点击左边的栏目切换
			swichMenu(item) {
				if (item.id == this.listQuery.cid) return;
				this.listQuery.cid = item.id;
				this.$nextTick(function(){
					this._initData(true)
				})
				// 如果为0，意味着尚未初始化
				// if (this.menuHeight == 0 || this.menuItemHeight == 0) {
				// 	await this.getElRect('menu-scroll-view', 'menuHeight');
				// 	await this.getElRect('u-tab-item', 'menuItemHeight');
				// }
				// // 将菜单菜单活动item垂直居中
				// this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			// getElRect(elClass, dataVal) {
			// 	new Promise((resolve, reject) => {
			// 		const query = uni.createSelectorQuery().in(this);
			// 		query.select('.' + elClass).fields({
			// 			size: true
			// 		}, res => {
			// 			// 如果节点尚未生成，res值为null，循环调用执行
			// 			if (!res) {
			// 				setTimeout(() => {
			// 					this.getElRect(elClass);
			// 				}, 10);
			// 				return;
			// 			}
			// 			this[dataVal] = res.height;
			// 		}).exec();
			// 	})
			// },
			// routes(data) {
			// 	uni.navigateTo({
			// 		url: data
			// 	});
			// },
		}
	}
</script>

<style lang="scss">
	.main-top {
		border-bottom: 2rpx #F5F5F5 solid;
		background-color: #FFFFFF;
		padding: 15rpx 30rpx;

		.cuIcon-search {
			margin: 20rpx;
			color: #666;
		}

		.weui-cell__bd {
			border-radius: 50rpx;
			height: 70rpx;
			display: flex;
			align-items: center;
			border: #FFFFFF 1rpx solid;
			background-color: #F5F5F5;
		}

		.placeholder-input {
			color: #666;
		}
	}

	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top) - 50px);
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 18rpx 30rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
		background-color: #FFFFFF;
	}

	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 175rpx;
		height: 100%;
		background: #F9F9F9;
	}

	.u-tab-item {
		height: 110rpx;
		background: #F9F9F9;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		// justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}

	.u-tab-item .u-line-1 {
		margin-left: 30rpx;
	}

	.u-tab-item-active {
		position: relative;
		color: #FE7E00;
		font-size: 28rpx;
		// background: #fff;
	}

	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid #FE7E00;
		height: 32rpx;
		left: 0;
		top: 39rpx;
	}

	.right-box {
		background-color: #fff;
		flex: 1;
	}

	.page-view {
		padding: 25rpx 16rpx;

		.page-view-box {
			// width: 220rpx;
			margin-left: 23rpx;
			margin-bottom: 20rpx;

			image {
				width: 242rpx;
				height: 156rpx;
				border-radius: 15rpx;
				margin-right: 25rpx;
			}
			.name{
				max-width: 245rpx;
				overflow:hidden; //超出的文本隐藏
				text-overflow:ellipsis; //溢出用省略号显示
				white-space:nowrap; //溢出不换行
			}
			.u-font-24{
				margin: 20rpx 0rpx;
			}
			.text-center {
				padding: 10rpx;
			}
		}
	}
</style>
